package com.dragon.client.controller.auth;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.auth.PositionService;
import com.dragon.repository.domain.dto.PositionDTO;
import com.dragon.repository.domain.vo.PositionVO;
import com.dragon.repository.entity.Admin;
import com.dragon.repository.entity.Position;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 岗位接口
 * @author Charlotte
 */
@RestController
@Api(tags = "岗位管理")
@RequestMapping("position")
public class PositionController {

    @Autowired
    PositionService service;

    @GetMapping("list")
    @ApiOperation("列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "岗位编码", required = false, dataType = "string"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, type = "string")
    })
    public ResponseData<List<PositionDTO>> listP(@RequestParam(required = false) Integer departmentId,
                                                 @RequestParam(required = false) String search){
        List<Position> list = service.ListP(departmentId,search);
        return ResponseData.success(PositionDTO.convertList(list));
    }

    @GetMapping("pageList")
    @ApiOperation(value = "分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId",required = true,type = "int",value = "部门id"),
            @ApiImplicitParam(name = "name",required = false,type = "string",value = "岗位名称"),
            @ApiImplicitParam(name = "page",required = true,type = "int",value = "页码"),
            @ApiImplicitParam(name = "limit",required = true,type = "int",value = "条数"),
    })
    public ResponseData<List<PositionDTO>> selectPageList(
            @RequestParam Integer departmentId,
            @RequestParam(required = false) String name,
            @RequestParam Integer page,
            @RequestParam Integer limit
            ){
        Page<Position> positionPage = service.selectPageList(departmentId,name,page,limit);
        return ResponseData.success(PositionDTO.convertList(positionPage.getRecords()),positionPage.getTotal());
    }


    @PostMapping("add")
    @ApiOperation("添加岗位")
    public ResponseData insert(@RequestBody @Validated(Insert.class) PositionVO vo){
        service.insert(PositionVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("update")
    @ApiOperation("更新岗位")
    public ResponseData update(@RequestBody @Validated(Update.class) PositionVO vo){
        service.update(PositionVO.convert(vo));
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除岗位")
    public ResponseData delete(@ApiParam(name = "positionId", value = "岗位id", required = true, type = "int") @RequestParam  Integer positionId){
        service.deleteById(positionId);
        return ResponseData.success();
    }

    @PostMapping("deleteByIds")
    @ApiOperation("批量删除")
    public ResponseData deleteByIds(@RequestParam
                                        @ApiParam(name = "ids", required = true, type = "string", value = "删除id集合") String ids){
        service.deleteBySomeIds(ids);
        return ResponseData.success();
    }

    @GetMapping("oneLevel")
    @ApiOperation("选择主管")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "grade", value = "级别", required = false, dataType = "int"),
            @ApiImplicitParam(name = "departmentId", value = "部门id", required = false, type = "string")
    })
    public ResponseData oneLevel( @RequestParam(required = false) Integer grade , @RequestParam(required = false) String departmentId ){
        List<Admin> admins = service.levelPermissions(grade,departmentId);
        return ResponseData.success(admins);
    }

    @GetMapping("chooseDepartment")
    @ApiOperation("选择部门")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "departmentId", value = "部门id", required = false, type = "string")
    })
    public ResponseData chooseDepartment(@RequestParam String departmentId){
        List<Admin> admins = service.chooseDepartment(departmentId);
        return ResponseData.success(admins);
    }
}
